package demo3;

import java.util.*;

public class Solution {

    public static int pivotIndex(int[] nums) {
        int n = nums.length, sum = nums[0];
        int[] dp = new int[n];
        for(int i = 1; i < n; i++){
            dp[i] = dp[i - 1] + nums[i - 1];
            sum += nums[i];
        }

        for(int i = 0; i < n; i++){
            if(dp[i] == sum - dp[i] - nums[i]){
                return i;
            }
        }
        return -1;
    }

    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] f = new int[n], g = new int[n];
        f[0] = 1;
        g[n - 1] = 1;

        for(int i = 1; i < n; i++){
            f[i] = f[i - 1] * nums[i - 1];
        }
        for(int i = n - 2; i >= 0; i--){
            g[i] = g[i + 1] * nums[i + 1];
        }

        int[] ret = new int[n];
        for(int i = 0; i < n; i++){
            ret[i] = f[i] * g[i];
        }
        return ret;
    }

    public static int subarraySum1(int[] nums, int k) {
        int n = nums.length, t = 0, sum = 0;
        for(int left = 0, right = 0; right < n; right++){
            sum += nums[right];
            while(sum > k){
                sum -= nums[left++];
            }
            if(sum == k) { t++;}
        }
        return t;
    }

    public static int subarraySum(int[] nums, int k) {
        Map<Integer, Integer> hash = new HashMap<>();
        hash.put(0, 1);

        int ret = 0 ,sum = 0;
        for(int x : nums){
            sum += x;
            ret += hash.getOrDefault(sum - k, 0);
            hash.put(sum, hash.getOrDefault(sum, 0) + 1);
        }
        return ret;
    }

    public int subarraysDivByK(int[] nums, int k) {
        Map<Integer, Integer> hash = new HashMap<>();
        hash.put(0 % k, 1);

        int ret = 0, sum = 0;
        for(int x : nums){
            sum += x;
            int r = (sum % k + k) % k;
            ret += hash.getOrDefault(r, 0);
            hash.put(r, hash.getOrDefault(r, 0) + 1);
        }
        return ret;
    }

    public int findMaxLength(int[] nums) {
        Map<Integer, Integer> hash = new HashMap<>();
        hash.put(0, -1);

        int sum = 0, ret = 0;
        for(int i = 0; i < nums.length; i++){
            sum += (nums[i] == 0 ? -1 : 1);
            if(hash.containsKey(sum)){
                ret = Math.max(ret, i - hash.get(sum));
            }else{
                hash.put(sum, i);
            }
        }
        return ret;
    }

    public int[][] matrixBlockSum(int[][] mat, int k) {
        int n = mat.length, m = mat[0].length;
        int[][] dp = new int[n + 1][m + 1];
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + mat[i - 1][j - 1];
            }
        }

        int[][] ret = new int[n][m];
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                int x1 = Math.max(0, i - k) + 1, y1 = Math.max(0, j - k) + 1;
                int x2 = Math.min(n - 1, i + k) + 1, y2 = Math.min(m - 1, j + k) + 1;
                ret[i][j] = dp[x2][y2] - dp[x2][y1 - 1] - dp[x1 - 1][y2] + dp[x1 - 1][y1 - 1];
            }
        }
        return ret;
    }


    public static void main(String[] args) {
//        int[] x = new int[]{1,7,3,6,5,6};
//        System.out.println(pivotIndex(x));

        int[] x = new int[]{1};
        System.out.println(subarraySum(x, 0));
    }
}
